<script setup lang="ts">
import {reactive, ref} from "vue";
import {ElMessage} from 'element-plus'
import {Plus} from '@element-plus/icons-vue'
import type {UploadProps} from 'element-plus'
import index from '@/components/Editor/index.vue'

const handleClick = () => {
    console.log('click')
}

const tableData = [
    {
        date: '2016-05-03',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: '',
    },
    {
        date: '2016-05-02',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: 'CA 90036',
    },
    {
        date: '2016-05-04',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: 'CA 90036',
    },
    {
        date: '2016-05-01',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: 'CA 90036',
    },
]
//添加文章
const dialogFormVisible = ref(false)

const form = reactive({
    name: '',
    region: '',
    date1: '',
    date2: '',
    delivery: false,
    type: [],
    resource: '',
    desc: '',
})
const formInline = reactive({
    user: '',
    region: '',
})

//上传图片
const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}

//文本框
const textarea = ref('')
</script>

<template>
    <div>
        <el-form-item>
            <el-button plain @click="dialogFormVisible = true">
                添加
            </el-button>
        </el-form-item>
        <!--        添加弹框-->
        <el-dialog v-model="dialogFormVisible" title="添加文章  " width="1000" center="true">
            <el-form :model="form">
                <el-form-item label="文章标题">
                    <el-input v-model="formInline.user" placeholder="请输入文章名称" clearable/>
                </el-form-item>
                <div class="windowSection">
                    <div class="sectionTop">
                        <el-form-item label="封面图片">
                            <el-upload
                                class="avatar-uploader"
                                action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload"
                            >
                                <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
                                <el-icon v-else class="avatar-uploader-icon">
                                    <Plus/>
                                </el-icon>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="选择文章栏目" style="margin-left: 50px">
                        </el-form-item>
                        <el-select
                            v-model="formInline.region"
                            placeholder="请选择"
                            clearable
                        >
                            <el-option label="Zone one" value="shanghai"/>
                            <el-option label="Zone two" value="beijing"/>
                            <el-option label="Zone one" value="shanghai"/>
                            <el-option label="Zone two" value="beijing"/>
                            <el-option label="Zone one" value="shanghai"/>
                        </el-select>
                    </div>
                    <el-form-item label="文章内容">
                    </el-form-item>
                    <index style="height: 200px;margin-bottom: 30px"></index>
                </div>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="dialogFormVisible = false">
                        保存
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-table :data="tableData" style="width: 100%;">
            <el-table-column prop="name" label="序号" width="300" align="center"/>
            <el-table-column prop="state" label="文章名称" width="300" align="center"/>
            <el-table-column prop="city" label="发布人" width="300" align="center"/>
            <el-table-column prop="address" label="创建时间" width="300" align="center"/>
            <el-table-column prop="zip" label="操作" width="300" align="center"/>
        </el-table>
        <el-pagination background layout="prev, pager, next" :total="100" class="page"/>
    </div>
</template>

<style scoped lang="scss">
.page {
    margin-top: 20px;
    margin-left: 1000px;
}

.sectionTop {
    display: flex;
}

//上传图片
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>

